﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;
using File = System.IO.File;

namespace Hiflo
{
    //定义委托
    public delegate void filter_dataTime(DateTime dt_start, DateTime dt_end);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //定义软件版本
        public string SOFTWARE_VERSION = "1.0.0";
        //配置文件
        public string m_cfgfilePath = Environment.CurrentDirectory + @"\config.bin";
        public string CFG_LANG = "language";

        public static int LANG_EN = 0;
        public static int LANG_CH = 1;
        public int m_language = LANG_EN;   //初始默认语言为英文

        public static int MODE_BABY = 0;  //儿童模式
        public static int MODE_ADULT = 1; //成人模式

        public static UInt16 SYSERROR_BIT = 0x8000;                 //系统错误
        public static UInt16 O2_TOO_HIGH = 0x0800;                  //氧浓度过高   (这个由上位机自己判定，不是下位机给的数据)
        public static UInt16 O2_TOO_LOW = 0X0400;                   //氧浓度过低   (这个由上位机自己判定，不是下位机给的数据)
        public static UInt16 LEAKAGE_BIT = 0x0080;                  //漏气
        public static UInt16 BLOCK_BIT = 0x0040;                    //堵塞
        public static UInt16 DEWPOINTTEMP_EXCEED_BIT = 0x0020;      //超温
        public static UInt16 HEATWIRE_BREAK = 0x0010;               //发热线脱落
        public static UInt16 LACKWATER_BIT = 0x0008;                //缺水
        public static UInt16 LOWTEMP_BIT = 0X0004;                  //温度达不到
        public static UInt16 LOWFLOW_BIT = 0x0002;                  //流量超范围
        public static UInt16 AMBIENTTEMP_EXCEED = 0X0001;           //环境温度超范围

        public static UInt16 O2SENSOR_ERR_BIT = 0X80;               //氧浓度传感器故障
        public static UInt16 FLOWSENSOR_ERR_BIT = 0X40;             //流量传感器故障
        public static UInt16 PRESSSENSOR_ERR_BIT = 0X20;            //压力传感器故障
        public static UInt16 HEATPLATE_NTC_ERR_BIT = 0X10;          //发热盘NTC故障
        public static UInt16 HEATPLATE_DROP_BIT = 0X08;             //发热盘未连接
        public static UInt16 HIGHO2BOARD_ERR_BIT = 0X04;            //空氧板故障
        public static UInt16 POWER_ERR_BIT = 0X02;                  //电源故障(5V
        public static UInt16 ZERO_CROSS_BIT = 0X01;                 //过零检测故障

        public DateTime m_dt_min = new DateTime();
        public DateTime m_dt_max = new DateTime();
        public DateTime m_dt_start = new DateTime();
        public DateTime m_dt_end = new DateTime();

        //报警信息头
        public class ALARM_HEAD
        {
            public byte[] file_flg = new byte[16];
            public UInt32 alarm_qty;
            public UInt16 crc_code;
            public byte[] reserv=new byte[10];
        }

        //报警数据
        public class ALARM_INFO
        {
            public byte year_L;
            public byte year_H;
            public byte month;
            public byte day;
            public byte hour;
            public byte minute;
            public byte second;
            public UInt16 alarm_code;
            public byte sys_err_code;
            public byte o2;
            public byte o2_high_limit;
            public byte o2_low_limit;
        }


        //工作信息头
        public class WORK_DATA_HEAD
        {
            public byte[] file_flg = new byte[16];
            public UInt32 workData_qty;
            public UInt16 crc_code;
            public byte[] reserve = new byte[10];
        }

        //工作信息数据
        public class WORK_DATA_INFO
        {
            public byte year_L;
            public byte year_H;
            public byte month;
            public byte day;
            public byte hour;
            public byte minute;
            public byte second;

            public byte flow;
            public byte temp;
            public byte o2;
            public byte o2_high_limit;
            public byte o2_low_limit;
            public byte mode;
        }

        //报警信息链表
        public List<ALARM_INFO> m_list_alarmInfo = new List<ALARM_INFO>();
        //工作数据链表
        public List<WORK_DATA_INFO> m_list_workDataInfo = new List<WORK_DATA_INFO>();

        //SD卡路径
        private string m_sdCard_path = "";

        //获取工作数据
        private void get_work_data_by(string workData_path)
        {
            FileStream fs = new FileStream(workData_path, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            int HEAD_SIZE = 32;
            byte[] head_buffer = new byte[HEAD_SIZE];
            br.Read(head_buffer, 0, HEAD_SIZE);

            //解析工作数据头
            //计算CRC校验码
            UInt16 crc_code = 0;
            for (int i = 0; i < 20; i++)
            {
                crc_code += (UInt16)head_buffer[i];
            }
            //UInt16 file_crc_code = Convert.ToUInt16(head_buffer[20] + head_buffer[21] * 256);

            UInt32 workData_qty = Convert.ToUInt32(head_buffer[16] + (head_buffer[17] << 8) + (head_buffer[18] << 16) + (head_buffer[19] << 24));
            FileInfo file_info = new FileInfo(workData_path);

            //如果文件标志和crc,以及文件大小对得上
            if (head_buffer[0] == 'w'
                && head_buffer[1] == 'o'
                && head_buffer[2] == 'r'
                && head_buffer[3] == 'k'
                && head_buffer[4] == 'd'
                && head_buffer[5] == 'a'
                && head_buffer[6] == 't'
                && head_buffer[7] == 'a'
                //&& file_crc_code == crc_code
                && (file_info.Length == HEAD_SIZE + workData_qty * 32)
                )
            {
                //一次性读取出来
                byte[] work_data_buffer = new byte[workData_qty * 32];
                br.Read(work_data_buffer, 0, (int)workData_qty * 32);

                //解析数据到链表中
                for (int i = 0; i < workData_qty; i++)
                {
                    WORK_DATA_INFO workDataInfo = new WORK_DATA_INFO();
                    int BASE_INDEX = (int)(workData_qty - 1 - i) * 32;
                    workDataInfo.year_L = work_data_buffer[BASE_INDEX + 0];
                    workDataInfo.year_H = work_data_buffer[BASE_INDEX + 1];
                    workDataInfo.month = work_data_buffer[BASE_INDEX + 2];
                    workDataInfo.day = work_data_buffer[BASE_INDEX + 3];
                    workDataInfo.hour = work_data_buffer[BASE_INDEX + 4];
                    workDataInfo.minute = work_data_buffer[BASE_INDEX + 5];
                    workDataInfo.second = work_data_buffer[BASE_INDEX + 6];
                    workDataInfo.flow = work_data_buffer[BASE_INDEX + 7];
                    workDataInfo.temp = work_data_buffer[BASE_INDEX + 8];
                    workDataInfo.o2 = work_data_buffer[BASE_INDEX + 9];
                    workDataInfo.o2_high_limit = work_data_buffer[BASE_INDEX + 10];
                    workDataInfo.o2_low_limit = work_data_buffer[BASE_INDEX + 11];
                    workDataInfo.mode = work_data_buffer[BASE_INDEX + 12];

                    //倒排添加
                    m_list_workDataInfo.Add(workDataInfo);
                }
            }
            else
            {
                MessageBox.Show("Alarm file data is not right!");
            }
            br.Close();
            fs.Close();
        }

        //获取报警数据
        private void get_alarm_data_by(string alarmData_path)
        {
            FileStream fs = new FileStream(alarmData_path, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            int HEAD_SIZE = 32;
            byte[] head_buffer = new byte[HEAD_SIZE];
            br.Read(head_buffer, 0, HEAD_SIZE);

            //计算CRC校验码
            UInt16 crc_code = 0;
            for (int i = 0; i < 20; i++)
            {
                crc_code += (UInt16)head_buffer[i];
            }
            //UInt16 file_crc_code = Convert.ToUInt16(head_buffer[20] + head_buffer[21] * 256);

            UInt32 alarm_qty = Convert.ToUInt32(head_buffer[16] + (head_buffer[17] << 8) + (head_buffer[18] << 16) + (head_buffer[19] << 24));
            FileInfo file_info = new FileInfo(alarmData_path);
            
            //如果文件标志和crc,以及文件大小对得上
            if (head_buffer[0] == 'a'
                && head_buffer[1] == 'l'
                && head_buffer[2] == 'a'
                && head_buffer[3] == 'r'
                && head_buffer[4] == 'm'
                //&& file_crc_code == crc_code
                && (file_info.Length== HEAD_SIZE+ alarm_qty*32)
                )
            {
                //一次性读取出来
                byte[] alarm_data_buffer = new byte[alarm_qty * 32];
                br.Read(alarm_data_buffer, 0, (int)alarm_qty * 32);

                //解析数据到链表中
                for (int i = 0; i < alarm_qty; i++)
                {
                    ALARM_INFO alarmInfo = new ALARM_INFO();
                    int BASE_INDEX = (int)(alarm_qty - 1 - i) * 32;
                    alarmInfo.year_L = alarm_data_buffer[BASE_INDEX + 0];
                    alarmInfo.year_H = alarm_data_buffer[BASE_INDEX + 1];
                    alarmInfo.month = alarm_data_buffer[BASE_INDEX + 2];
                    alarmInfo.day = alarm_data_buffer[BASE_INDEX + 3];
                    alarmInfo.hour = alarm_data_buffer[BASE_INDEX + 4];
                    alarmInfo.minute = alarm_data_buffer[BASE_INDEX + 5];
                    alarmInfo.second = alarm_data_buffer[BASE_INDEX + 6];
                    alarmInfo.alarm_code = Convert.ToUInt16(alarm_data_buffer[BASE_INDEX + 7] | (alarm_data_buffer[BASE_INDEX + 8] << (byte)8));
                    alarmInfo.sys_err_code = alarm_data_buffer[BASE_INDEX + 9];
                    alarmInfo.o2 = alarm_data_buffer[BASE_INDEX + 10];
                    alarmInfo.o2_high_limit = alarm_data_buffer[BASE_INDEX + 11];
                    alarmInfo.o2_low_limit = alarm_data_buffer[BASE_INDEX + 12];

                    //倒排添加
                    m_list_alarmInfo.Add(alarmInfo);
                }
                //MessageBox.Show(m_list_alarmInfo.Count.ToString());
            }
            else
            {
                MessageBox.Show("Alarm file data is not right!");
            }

            br.Close();
            fs.Close();
        }

        public string get_string_by_alarm_code(UInt16 alarmCode)
        {
            string str = "";
            if (alarmCode == SYSERROR_BIT)
            {
                str = LanguageMngr.system_error();
            }
            else if (alarmCode == O2_TOO_HIGH)
            {
                str = LanguageMngr.o2_too_high();
            }
            else if (alarmCode == O2_TOO_LOW)
            {
                str = LanguageMngr.o2_too_low();
            }
            else if (alarmCode == LEAKAGE_BIT)
            {
                str = LanguageMngr.leakage();
            }
            else if (alarmCode == BLOCK_BIT)
            {
                str = LanguageMngr.block();
            }
            else if (alarmCode == DEWPOINTTEMP_EXCEED_BIT)
            {
                str = LanguageMngr.over_temp();
            }
            else if (alarmCode == HEATWIRE_BREAK)
            {
                str = LanguageMngr.heatwire_break();
            }
            else if (alarmCode == LACKWATER_BIT)
            {
                str = LanguageMngr.lack_of_water();
            }
            else if (alarmCode == LOWTEMP_BIT)
            {
                str = LanguageMngr.cant_reach_target_temp();
            }
            else if (alarmCode == LOWFLOW_BIT)
            {
                str = LanguageMngr.flow_out_of_range();
            }
            else if (alarmCode == AMBIENTTEMP_EXCEED)
            {
                str = LanguageMngr.ambient_temp_out_of_range();
            }
            else
            {
                //do nothing
            }

            return str;
        }

        public void update_alarm_listview_by(DateTime dt_start,DateTime dt_end)
        {
            m_dt_start = dt_start;
            m_dt_end = dt_end;
            this.listView_alarm.Items.Clear();

            this.listView_alarm.Columns.Clear();
            this.listView_alarm.View = View.Details;
            this.listView_alarm.Columns.Add("No.", 50, HorizontalAlignment.Left);
            this.listView_alarm.Columns.Add(LanguageMngr.date_time(), 180, HorizontalAlignment.Left);
            this.listView_alarm.Columns.Add(LanguageMngr.alarm(), 200, HorizontalAlignment.Left);
            this.listView_alarm.Columns.Add(LanguageMngr.value(), 260, HorizontalAlignment.Left);

            this.listView_alarm.BeginUpdate();

            int index = 0;
            for (int i = 0; i < m_list_alarmInfo.Count; i++)
            {
                DateTime dt = new DateTime(m_list_alarmInfo[i].year_H * 100 + m_list_alarmInfo[i].year_L, 
                                            m_list_alarmInfo[i].month, 
                                            m_list_alarmInfo[i].day, 
                                            m_list_alarmInfo[i].hour, 
                                            m_list_alarmInfo[i].minute, 
                                            m_list_alarmInfo[i].second);
                if (dt >= dt_start && dt <= dt_end)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = (index + 1).ToString();       //No.
                    index++;
                    lvi.SubItems.Add(dt.ToString("yyyy/MM/dd HH:mm:ss"));  //日期
                    lvi.SubItems.Add(get_string_by_alarm_code(m_list_alarmInfo[i].alarm_code));
                    string str_val = "";
                    if (m_list_alarmInfo[i].alarm_code == O2_TOO_HIGH || m_list_alarmInfo[i].alarm_code == O2_TOO_LOW)
                    {
                        str_val = LanguageMngr.o2_concentrate() + ":" + m_list_alarmInfo[i].o2.ToString() + "%" + ","
                                    + LanguageMngr.o2_concentrate_range() + ":" + m_list_alarmInfo[i].o2_low_limit + "%" + "-" + m_list_alarmInfo[i].o2_high_limit + "%";
                    }
                    lvi.SubItems.Add(str_val);

                    this.listView_alarm.Items.Add(lvi);
                }
            }

            //this.listView_alarm.Invalidate();
            this.listView_alarm.EndUpdate();
        }

        public void update_workData_listview_by(DateTime dt_start,DateTime dt_end)
        {
            m_dt_start = dt_start;
            m_dt_end = dt_end;
            this.listView_work_data.Items.Clear();

            this.listView_work_data.Columns.Clear();
            this.listView_work_data.View = View.Details;
            this.listView_work_data.Columns.Add("No.", 50, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.date_time(), 180, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.flow(), 120, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.temperature(), 120, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.O2_Percent(), 120, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.O2HighLimit_Percent(), 120, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.O2LowLimit_Percent(), 120, HorizontalAlignment.Left);
            this.listView_work_data.Columns.Add(LanguageMngr.mode(), 120, HorizontalAlignment.Left);

            //更新数据
            int index = 0;
            this.listView_work_data.BeginUpdate();
            for (int i = 0; i < m_list_workDataInfo.Count; i++)
            {
                DateTime dt = new DateTime(m_list_workDataInfo[i].year_H * 100 + m_list_workDataInfo[i].year_L,
                                            m_list_workDataInfo[i].month,
                                            m_list_workDataInfo[i].day,
                                            m_list_workDataInfo[i].hour,
                                            m_list_workDataInfo[i].minute,
                                            m_list_workDataInfo[i].second);
                if (dt >= dt_start && dt <= dt_end)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = (index + 1).ToString();
                    index++;
                    lvi.SubItems.Add(dt.ToString("yyyy/MM/dd HH:mm:ss"));       //日期
                    lvi.SubItems.Add(m_list_workDataInfo[i].flow.ToString());   //流量
                    lvi.SubItems.Add(m_list_workDataInfo[i].temp.ToString());   //温度
                    lvi.SubItems.Add(m_list_workDataInfo[i].o2.ToString());     //氧浓度
                    lvi.SubItems.Add(m_list_workDataInfo[i].o2_high_limit.ToString());     //氧浓度上限
                    lvi.SubItems.Add(m_list_workDataInfo[i].o2_low_limit.ToString());     //氧浓度下限                                                                     
                    string str_mode = "";                                                   //模式
                    if (m_list_workDataInfo[i].mode == MODE_BABY)
                    {
                        str_mode = LanguageMngr.low_flow_mode();
                    }
                    else if (m_list_workDataInfo[i].mode == MODE_ADULT)
                    {
                        str_mode = LanguageMngr.high_flow_mode();
                    }
                    else
                    {
                        //do nothing
                    }
                    lvi.SubItems.Add(str_mode);     //氧浓度下限

                    listView_work_data.Items.Add(lvi);
                }
            }
            this.listView_work_data.EndUpdate();
        }

        private void button_open_Click(object sender, EventArgs e)
        {
            //folderBrowserDialog1.ShowDialog(this)=
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                m_list_alarmInfo.Clear();
                m_list_workDataInfo.Clear();

                m_sdCard_path = folderBrowserDialog1.SelectedPath;
                this.textBox_sdCard_path.Text = m_sdCard_path;
                //检查SD卡中是否有"alarm.dat"和"work_data.dat"
                string work_data_path = m_sdCard_path + "work_data.dat";
                if (!File.Exists(work_data_path))
                {
                    //如果没有工作数据
                    MessageBox.Show(LanguageMngr.no_work_data_found());
                    return;
                }
                //获取工作数据
                get_work_data_by(work_data_path);

                string alarm_data_path = m_sdCard_path + "alarm.dat";
                if (!File.Exists(alarm_data_path))
                {
                    MessageBox.Show(LanguageMngr.no_alarm_data_found());
                }
                else
                {
                    //获取报警数据
                    get_alarm_data_by(alarm_data_path);
                }

                //添加报警列表
                if (m_list_alarmInfo != null && m_list_alarmInfo.Count > 0)
                {
                    ALARM_INFO alamInfo = m_list_alarmInfo[0];
                    DateTime dt_end = new DateTime(alamInfo.year_H * 100 + alamInfo.year_L,alamInfo.month,alamInfo.day, 23, 59,59);
                    alamInfo = m_list_alarmInfo[m_list_alarmInfo.Count - 1];
                    DateTime dt_start = new DateTime(alamInfo.year_H * 100 + alamInfo.year_L,alamInfo.month,alamInfo.day,0,0,0);
                    update_alarm_listview_by(dt_start, dt_end);
                }

                //添加工作列表
                if (m_list_workDataInfo != null && m_list_workDataInfo.Count > 0)
                {
                    WORK_DATA_INFO workInfo = m_list_workDataInfo[0];
                    DateTime dt_end = new DateTime(workInfo.year_H * 100 + workInfo.year_L,workInfo.month,workInfo.day, 23, 59, 59);
                    workInfo = m_list_workDataInfo[m_list_workDataInfo.Count-1];
                    DateTime dt_start = new DateTime(workInfo.year_H * 100 + workInfo.year_L,workInfo.month, workInfo.day,0,0,0);
                    update_workData_listview_by(dt_start, dt_end);
                }
            }
        }
        
        public void read_config_file()
        {
            //如果有文件
            if (File.Exists(m_cfgfilePath))
            {
                StreamReader sr = new StreamReader(m_cfgfilePath,Encoding.UTF8);

                string read_str = sr.ReadLine();
                while (read_str != null)
                {
                    if (read_str.IndexOf(CFG_LANG) >= 0)
                    {
                        m_language = Convert.ToInt32(read_str.Substring(read_str.LastIndexOf("=")+1));
                    }
                    read_str = sr.ReadLine();
                }
                sr.Close();
            }
            else
            {
                m_language = LANG_EN;
            }
        }

        public void init_sys_language()
        {
            //获取语言
            LanguageMngr.m_lang = m_language;

            this.label_sdCard_path.Text = LanguageMngr.sdCard_path();
            this.文件ToolStripMenuItem.Text = LanguageMngr.file();
            this.数据保存ToolStripMenuItem.Text = LanguageMngr.save_data();
            this.其他ToolStripMenuItem.Text = LanguageMngr.others();
            this.语言ToolStripMenuItem.Text = LanguageMngr.language();
            this.中文ToolStripMenuItem.Text = LanguageMngr.Chinese();
            this.英文ToolStripMenuItem.Text = LanguageMngr.English();
            this.软件版本ToolStripMenuItem.Text = LanguageMngr.software_version();

            this.tabControl1.TabPages[0].Text = LanguageMngr.data();
            this.tabControl1.TabPages[1].Text = LanguageMngr.alarm();

            this.button_open.Text = LanguageMngr.open();
            this.button_filter.Text = LanguageMngr.filter();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            read_config_file();  //读取配置文件

            init_sys_language();  //初始化系统语言
        }

        private void button_filter_Click(object sender, EventArgs e)
        {
            if (m_list_workDataInfo != null && m_list_workDataInfo.Count > 0)
            {
                DateTimeFilter dt_filter_form = new DateTimeFilter();
                //DateTime dt_start = new DateTime();
                //DateTime dt_end = new DateTime();
                WORK_DATA_INFO workInfo = m_list_workDataInfo[0];  //第一个元素就是结束的时间(倒排)
                DateTime dtMax = new DateTime(workInfo.year_L + workInfo.year_H * 100,
                                                workInfo.month,workInfo.day,
                                                23, 59, 59);
                
                workInfo = m_list_workDataInfo[m_list_workDataInfo.Count - 1];  //最后一个元素就是开始的时间(倒排)
                DateTime dtMin = new DateTime(workInfo.year_L + workInfo.year_H * 100,
                                                workInfo.month, workInfo.day,
                                                0, 0, 0);
                //获取时间的开始和结束
                m_dt_min = dtMin;
                m_dt_max = dtMax;

                if (m_dt_start <= m_dt_min)
                {
                    m_dt_start = m_dt_min;
                }
                if (m_dt_end >= m_dt_max|| m_dt_end< m_dt_min)
                {
                    m_dt_end = m_dt_max;
                }
                //过滤窗中的委托，挂入主窗的处理函数
                dt_filter_form.filterDateTimeHandler += update_list_view_by;
                dt_filter_form.init_by(m_dt_start, m_dt_end, m_dt_min, m_dt_max);
                dt_filter_form.ShowDialog();
            }
            else
            {
                MessageBox.Show(LanguageMngr.no_data_found_pls_import_sdCard());
            }
        }

        public void update_list_view_by(DateTime dt_start, DateTime dt_end)
        {
            //过滤查找
            if (m_list_workDataInfo != null && m_list_workDataInfo.Count > 0)
            {
                m_dt_start = dt_start;
                m_dt_end = dt_end;
                update_workData_listview_by(dt_start, dt_end);
                update_alarm_listview_by(dt_start, dt_end);
            }
        }

        private void 软件版本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about_form = new About();
            about_form.Text = LanguageMngr.about();
            about_form.init_software_version(SOFTWARE_VERSION);
            about_form.ShowDialog();
        }

        private void 数据保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_list_workDataInfo != null && m_list_workDataInfo.Count > 0)
            {
                
                if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    WORK_DATA_INFO workInfo = m_list_workDataInfo[0];  //第一个元素就是结束的时间(倒排)
                    DateTime dtMax = new DateTime(workInfo.year_L + workInfo.year_H * 100,
                                                    workInfo.month, workInfo.day,
                                                    23, 59, 59);

                    workInfo = m_list_workDataInfo[m_list_workDataInfo.Count - 1];  //最后一个元素就是开始的时间(倒排)
                    DateTime dtMin = new DateTime(workInfo.year_L + workInfo.year_H * 100,
                                                    workInfo.month, workInfo.day,
                                                    0, 0, 0);
                    //获取时间的开始和结束
                    m_dt_min = dtMin;
                    m_dt_max = dtMax;

                    if (m_dt_start <= m_dt_min)
                    {
                        m_dt_start = m_dt_min;
                    }
                    if (m_dt_end >= m_dt_max || m_dt_end < m_dt_min)
                    {
                        m_dt_end = m_dt_max;
                    }

                    string workData_path = this.folderBrowserDialog1.SelectedPath + "workdata.csv";
                    string alarmData_path = this.folderBrowserDialog1.SelectedPath + "alarm.csv";

                    //写工作数据
                    #region
                    StreamWriter sw = new StreamWriter(workData_path, append: false, Encoding.UTF8);
                    int index = 0;
                    sw.WriteLine("No." + "," + LanguageMngr.date_time() + "," + LanguageMngr.flow() + "," + LanguageMngr.temperature() + ","
                        + LanguageMngr.O2_Percent() + "," + LanguageMngr.O2HighLimit_Percent() + "," + LanguageMngr.O2LowLimit_Percent() + "," + LanguageMngr.mode());
                    for (int i = 0; i < m_list_workDataInfo.Count; i++)
                    {
                        DateTime dt = new DateTime(m_list_workDataInfo[i].year_H * 100 + m_list_workDataInfo[i].year_L,
                                            m_list_workDataInfo[i].month,
                                            m_list_workDataInfo[i].day,
                                            m_list_workDataInfo[i].hour,
                                            m_list_workDataInfo[i].minute,
                                            m_list_workDataInfo[i].second);
                        
                        if (dt >= m_dt_start && dt <= m_dt_end)
                        {                                   
                            string str_mode = "";                                                  
                            if (m_list_workDataInfo[i].mode == MODE_BABY)
                            {
                                str_mode = LanguageMngr.low_flow_mode();
                            }
                            else if (m_list_workDataInfo[i].mode == MODE_ADULT)
                            {
                                str_mode = LanguageMngr.high_flow_mode();
                            }
                            string str_val = (++index).ToString() + ","
                                + dt.ToString("yyyy/MM/dd HH:mm:ss") + ","
                                + m_list_workDataInfo[i].flow.ToString() + ","
                                + m_list_workDataInfo[i].temp.ToString() + ","
                                + m_list_workDataInfo[i].o2.ToString() + ","
                                + m_list_workDataInfo[i].o2_high_limit.ToString() + ","
                                + m_list_workDataInfo[i].o2_low_limit.ToString() + ","
                                + str_mode;
                            sw.WriteLine(str_val);
                        }
                    }
                    sw.Close();
                    #endregion

                    //写报警数据
                    #region
                    sw = new StreamWriter(alarmData_path, append: false, Encoding.UTF8);
                    index = 0;
                    sw.WriteLine("No." + "," + LanguageMngr.date_time() + "," + LanguageMngr.alarm() + "," + LanguageMngr.value());
                    for (int i = 0; i < m_list_alarmInfo.Count; i++)
                    {
                        DateTime dt = new DateTime(m_list_alarmInfo[i].year_H * 100 + m_list_alarmInfo[i].year_L,
                                            m_list_alarmInfo[i].month,
                                            m_list_alarmInfo[i].day,
                                            m_list_alarmInfo[i].hour,
                                            m_list_alarmInfo[i].minute,
                                            m_list_alarmInfo[i].second);

                        if (dt >= m_dt_start && dt <= m_dt_end)
                        {
                            string str_O2 = "";
                            if (m_list_alarmInfo[i].alarm_code == O2_TOO_HIGH || m_list_alarmInfo[i].alarm_code == O2_TOO_LOW)
                            {
                                str_O2 = LanguageMngr.o2_concentrate()+":" + m_list_alarmInfo[i].o2.ToString() + "%"+";"
                                    + LanguageMngr.o2_concentrate_range()+":" + m_list_alarmInfo[i].o2_low_limit + "%" + "-" + m_list_alarmInfo[i].o2_high_limit + "%";
                            }

                            string str_val = (++index).ToString() + ","
                                + dt.ToString("yyyy/MM/dd HH:mm:ss") + ","
                                + get_string_by_alarm_code(m_list_alarmInfo[i].alarm_code) + ","
                                + str_O2;
                            sw.WriteLine(str_val);
                        }
                    }
                    sw.Close();
                    #endregion

                    MessageBox.Show(LanguageMngr.data_save_successful());
                }
            }
            else
            {
                MessageBox.Show(LanguageMngr.no_data());
            }
        }

        private void 中文ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_language = LANG_CH;
            init_sys_language();

            update_list_view_by(m_dt_start, m_dt_end);
        }

        private void 英文ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_language = LANG_EN;
            init_sys_language();

            update_list_view_by(m_dt_start, m_dt_end);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            save_cfg_file();  //保存配置文件
        }

        public void save_cfg_file()
        {
            StreamWriter sw = new StreamWriter(m_cfgfilePath, append: false, Encoding.UTF8);

            sw.WriteLine(CFG_LANG + "=" + Convert.ToString(Convert.ToInt32(m_language)));
 
            sw.Close();

        }
    }
}
